package com.shenhaofeng.permission;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.shenhaofeng.permissions.PermissionsListener;
import com.shenhaofeng.permissions.PermissionsRequester;
import com.shenhaofeng.permissions.PermissionsSource;

import java.util.List;

/**
 * Created by haofeng on 2016/9/25.
 */

public class MainActivity extends BaseActivity implements PermissionsListener {

    private static final int REQ_PER_CAMERA = 1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button button = new Button(this);
        setContentView(button);
        button.setText("打开相机");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openCamera();
            }
        });
    }


    private void openCamera() {
        PermissionsRequester requester = new PermissionsRequester.Builder()
                .listener(this)
                .reqRationale("我需要调用相机来获取图片")
                .source(this)
                .requestCode(1)
                .build();
        requester.requestPermissions(Manifest.permission.CAMERA,Manifest.permission.CALL_PHONE);
    }


    @Override
    public void onPermissionResult(int requestCode, List<String> grantedList, List<String> deniedList) {
        if (grantedList.contains(Manifest.permission.CAMERA)&&grantedList.contains(Manifest.permission.CALL_PHONE)) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivity(intent);
        }else{
            Toast.makeText(this, "需要相机和拨打电话的权限", Toast.LENGTH_LONG).show();
        }
    }
}
